package com.bw.message.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Scanner;

public class DeepSeekChatLA {
    private static final String API_KEY="sk-a64d8afb6e2d4ee5a9efb4bae7c8df8c";
    private static final String BASE_URL="https://api.deepseek.com";

    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        JSONArray messages = new JSONArray();

        while (true){
            //显示菜单
            System.out.println("请输入: 1提问 2退出");
            String choice = scanner.nextLine();
            if (choice.equals("1")){
                //提问 用户输入内容
                System.out.println("请输入您的问题:");
                String userQuestion = scanner.nextLine();
                //用户消费添加到消息数组
                JSONObject userMessage = new JSONObject();
                userMessage.put("role","user");
                userMessage.put("content",userQuestion);
                messages.add(userMessage);

                //获取助手的回应
              JSONObject assiostanResponse=  sendMessage(messages);
              messages.add(assiostanResponse);

              //输出对话内容
                System.out.println("用户:"+userQuestion);
                System.out.println("助手:"+assiostanResponse.getString("content"));

            }else if (choice.equals("2")){
                //退出程序
                System.out.println("退出对话程序");
                break;
            }else {
                //处理无效输入
                System.out.println("无效输入: 请输入1 或 2");
            }
        }

        scanner.close();
    }
    //发送消息并获取助手的响应
    private static JSONObject sendMessage(JSONArray messages) throws IOException {
        String url=  BASE_URL+"/chat/completions";
        //创建请求的json数据
        JSONObject payload = new JSONObject();
        payload.put("model","deepseek-chat");
        payload.put("messages",messages);

        //初始化 HttpClient 和 HttpPost
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost postMethod = new HttpPost(url);

        //设置请求头
        postMethod.setHeader("Content-Type","application/json");
        postMethod.setHeader("Authorization","Bearer"+API_KEY);

        //设置请求体为 json 数据
        StringEntity entity = new StringEntity(payload.toString(), "UTF-8");
        postMethod.setEntity(entity);

        //执行请求
        HttpResponse response = client.execute(postMethod);
        int statusCode = response.getStatusLine().getStatusCode();

        if (statusCode!=200){
            throw  new RemoteException("HTTP 请求失败，状态码:"+statusCode);
        }

        //获取响应并解析
        String responseString= EntityUtils.toString(response.getEntity());
        JSONObject responseJson = JSONObject.parseObject(responseString);
        JSONObject assistanMessage = responseJson.getJSONArray("choices")
                .getJSONObject(0)
                .getJSONObject("message");

        return assistanMessage;
    }

}
